package top.kaoshanji.p2ch06b;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 代码清单 p2ch06b-01：通过命名捕获分组来匹配字符串并提取内容的示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/17 19:41
 */
public class RegexExample01 {

    private static final Logger logger = LogManager.getLogger(RegexExample01.class);

    public static void main(String[] args) {
        logger.info("...代码清单 p2ch06b-01：通过命名捕获分组来匹配字符串并提取内容的示例...");

        String url = "http://www.example.org/uid/alex/docid/1/title/My+First+Blog";
        Pattern pattern = Pattern.compile("^.*/uid/(?<uid>.*)/docid/(?<docid>.*)/title/(?<title>.*)");
        Matcher matcher = pattern.matcher(url);
        if (matcher.matches()) {
            String uid = matcher.group("uid");
            String docid = matcher.group("docid");
            String title = matcher.group("title");

            logger.info("...uid:{}...", uid);
            logger.info("...docid:{}...", docid);
            logger.info("...title:{}...", title);

        }

    }

}
